package top.syfy.api.jdcloud.joke.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.syfy.api.jdcloud.joke.entity.JokeDO;
import top.syfy.api.jdcloud.joke.entity.PageDO;
import top.syfy.api.jdcloud.joke.entity.dto.PageDTO;
import top.syfy.api.jdcloud.joke.response.ContentReturnType;
import top.syfy.api.jdcloud.joke.service.JokeService;
import top.syfy.api.jdcloud.joke.util.AppKeyUtil;
import top.syfy.api.jdcloud.joke.wx.WxApiCall.WxApiCall;
import top.syfy.api.jdcloud.joke.wx.model.RequestModel;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取指定对象的分页数据服务层应用的实现
 * V - 分页查询参数对象
 *
 * @author awg
 * @date 2020/10/30 星期五
 */
@Service
public class JokeServiceImpl implements JokeService<PageDTO<JokeDO>> {

    @Autowired
    private AppKeyUtil appKeyUtil;

    /**
     * 获取指定对象的分页数据
     * @param pageDTO       分页查询参数对象(包含指定查询条件)
     * @return 指定对象的分页数据
     * @throws JsonProcessingException JSON解析异常
     */
    @Override
    public ContentReturnType listEntityPage(PageDTO<JokeDO> pageDTO) throws JsonProcessingException {
        RequestModel model = new RequestModel();
        model.setGwUrl(appKeyUtil.getApiURL());
        model.setAppKey(appKeyUtil.getAppKey());
        Map<String, Object> queryMap = new HashMap<>();
        // 访问参数
        queryMap.put("time", pageDTO.getT().getCt());
        // 访问参数
        queryMap.put("page", pageDTO.getCurrentPage());
        // 访问参数
        queryMap.put("maxResult", pageDTO.getMaxResult());
        model.setQueryParams(queryMap);
        // 发送请求
        WxApiCall call = new WxApiCall();
        call.setModel(model);
        // 接收请求返回的数据(JSON字符串)
        String jsonStr = call.request();

        ObjectMapper mapper = new ObjectMapper();
        JsonNode baseNode = mapper.readTree(jsonStr);
        //获取返回基本数据: 状态码 状态信息 API扣费标识
        String code = String.valueOf(baseNode.get("code")).replaceAll("\"", "");
        String msg = String.valueOf(baseNode.get("msg")).replaceAll("\"", "");
        Boolean charge = Boolean.parseBoolean(baseNode.get("charge").toString());
        //获取分页数据: 总记录数 总页数 每一页返回的最大结果集 当前页 上一页 下一页
        JsonNode resultNode = mapper.readTree(baseNode.get("result").toString());
        JsonNode showAPIResBodyNode = mapper.readTree(resultNode.get("showapi_res_body").toString());
        // 总记录数
        Long allNum = Long.valueOf(showAPIResBodyNode.get("allNum").toString());
        // 总页数
        Long allPages = Long.valueOf(showAPIResBodyNode.get("allPages").toString());
        // 每一页返回的最大结果集
        Long maxResult = Long.valueOf(showAPIResBodyNode.get("maxResult").toString());
        // 当前页
        Long currentPage = Long.valueOf(showAPIResBodyNode.get("currentPage").toString());
        // 上一页
        Long prePage = currentPage.equals(1L) ? 1L : currentPage - 1L;
        // 下一页
        Long nextPage = currentPage.equals(allPages) ? allPages: currentPage + 1L;
        // 获取内容列表
        List<JokeDO> list = mapper.readValue(showAPIResBodyNode.get("contentlist").toString(), new TypeReference<List<JokeDO>>() {
            @Override
            public Type getType() {
                return super.getType();
            }
        });
        // 设置数据
        PageDO<JokeDO> pageDO = new PageDO<>();
        pageDO.setAllNum(allNum);
        pageDO.setAllPages(allPages);
        pageDO.setMaxResult(maxResult);
        pageDO.setCurrentPage(currentPage);
        pageDO.setPrePage(prePage);
        pageDO.setNextPage(nextPage);
        pageDO.setT(list);
        return ContentReturnType.of(code, msg, charge, pageDO);
    }

}
